import deepMerge from "./deepMerge";
import deepClone from "./deepMerge";

export function keepOneDecimalFull(num) {
    var result = parseFloat(num);
    if (isNaN(result)) {
        return 0;
    }
    result = Math.round(num * 100) / 100;
    var s_x = result.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 1) {
        s_x += '0';
    }
    return s_x;
}

export function keepTwoDecimalFull(num) {
    var result = parseFloat(num);
    if (isNaN(result)) {
        return 0;
    }
    result = Math.round(num * 100) / 100;
    var s_x = result.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

export function is_weixin(){
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {//微信
        return true;
    }else if(ua.match(/wxwork/i) == 'wxwork'){ //企业微信
        return true;
    }else {
        return false;
    }

}

export function enterToBr(content){
    if(content){
        content = content.replace(/\r\n/g,"<br/>");
        
    }
    return content;
}

var common = {
    getCondtions:function (vals) {
        let conditions = [];
        for (let key in vals) {
            let val = vals[key]["value"]
            console.log("key:",key)
            let operate = vals[key]["operate"]
            if (val) {
                conditions.push({
                    name: key,
                    operate: !operate ? "like" : operate,
                    value: val,
                });
            }
        }
        return conditions ;
    },
    deepMerge(target = {}, source = {}){
        return deepMerge(target,source);
    },
    getCodeAndNames:function(data,option){
        let result = {code:"",name:""};    
        if(data && option){
            let codes = [],names = [];
            data.forEach((item,index,arr)=>{
                codes.push(item[option.code]);
                names.push(item[option.name]);
            })
            result.code = codes.join();
            result.name = names.join();
        }
        return result;
    }
}

export default common
